package com.example.schoolhelp.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.schoolhelp.R;

/**
 * Created by Administrator on 2017/7/15.
 */
public class MyToast {
    private static Toast mToast;

    private MyToast(Context context, CharSequence text, int duration) {
        View v = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
//        TextView textView = (TextView) v.findViewById(R.id.tv_content);
//        textView.setText(text);
        mToast = new Toast(context);
        mToast.setDuration(duration);
        mToast.setView(v);
    }

    private static MyToast makeText(Context context, CharSequence text, int duration) {
        return new MyToast(context, text, duration);
    }

    public static void showToast(Context context, CharSequence text, int duration) {
        if (mToast == null) {
            makeText(context, text, duration);
        }
        TextView textView = (TextView) mToast.getView().findViewById(R.id.tv_content);
        textView.setBackgroundResource(R.drawable.gery_bg_corner);
        textView.setText(text);
        mToast.show();
    }

    public static void showToast(Context context, CharSequence text) {
        if (mToast == null) {
            makeText(context, text, 2000);
        }
        TextView textView = (TextView) mToast.getView().findViewById(R.id.tv_content);
        textView.setBackgroundResource(R.drawable.gery_bg_corner);
        textView.setText(text);
        mToast.show();
    }

    public static void showErrorToast(Context context, CharSequence text, int duration) {
        if (mToast == null) {
            makeText(context, text, duration);
        }
        TextView textView = (TextView) mToast.getView().findViewById(R.id.tv_content);
        textView.setBackgroundResource(R.drawable.red_bg_corner);
        textView.setText(text);
        mToast.show();
    }

    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }

    public void setGravity(int gravity, int xOffset, int yOffset) {
        if (mToast != null) {
            mToast.setGravity(gravity, xOffset, yOffset);
        }
    }
}
